package com.youlu.campus.service.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author : [dengbo]
 * @className : ZmDateUtils
 * @description : [描述说明该类的功能]
 * @createTime : [2022/11/22 18:56]
 */
@Slf4j
public class ZmDateUtils {

    public static int getCalLeaveDays(Date startTime, Date endTime, List<String> calDateStrList) {
        //从startTime开始循环，若该日期不是节假日或者不是周六日则请假天数+1
        log.info("startTime:{},endTime:{} start", startTime, endTime);
        Date flag = startTime;
        //设置循环开始日期
        Calendar cal = Calendar.getInstance();
        int diff = 0;
        boolean isFirst = true;
        //循环遍历每个日期
        while (flag.compareTo(endTime) != 1) {
            cal.setTime(flag);
            // 排除第一天
            if (isFirst) {
                isFirst = false;
                cal.add(Calendar.DAY_OF_MONTH, +1);
                flag = cal.getTime();
                continue;
            }
            //判断是否为周六日
            int week = cal.get(Calendar.DAY_OF_WEEK);
            //1为周日，7为周六
            if (week == Calendar.SATURDAY || week == Calendar.SUNDAY) {
                log.info("date:{} is week", cal.getTime());
                //跳出循环进入下一个日期
                cal.add(Calendar.DAY_OF_MONTH, +1);
                flag = cal.getTime();
                continue;
            }
            // 如果是法定节假日去除
            String dateStr = ZmDateUtils.getStrByDate(cal.getTime());
            if (calDateStrList.contains(dateStr)) {
                log.info("date:{} is cal date", cal.getTime());
                cal.add(Calendar.DAY_OF_MONTH, +1);
                flag = cal.getTime();
                continue;
            }
            // 非周六和节假日+1
            diff++;
            cal.add(Calendar.DAY_OF_MONTH, +1);
            flag = cal.getTime();
        }
        log.info("starttime:{} ,endtime:{} end, diff:{}", startTime, endTime, diff);
        return diff;
    }

    public static Date getZeroNextDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        // 时
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        // 分
        calendar.set(Calendar.MINUTE, 0);
        // 秒
        calendar.set(Calendar.SECOND, 0);
        // 毫秒
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getZeroDate(Date date) {
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = dft.format(date);
        try {
            return dft.parse(dateStr);
        } catch (ParseException e) {
            log.error("date convert error", e);
        }
        return null;
    }

    public static long getTwoDateDiffDay(Date date1,Date date2) {
        log.info("date1:{},date2:{}",date1,date2);
        return Math.abs(date1.getTime()-date2.getTime())/(1000*3600*24);
    }

    public static Date getDateByStr(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            log.error("date convert error", e);
        }
        return null;
    }

    public static String getLocalDateStr() {
        LocalDate localDate = LocalDate.now();
        return localDate.toString();
    }

    public static Long getLongByLocalDate(LocalDate date) {
        return date.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static Date getDateByLocalDate(LocalDate date) {
        Instant instant = date.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

    public static String getStrByDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }
}
